package cn.chengpengper.transfer.config;

import cn.chengpengper.transfer.storage.TranslatorSetting;
import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.ui.JBColor;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

public class TranslatorSettingConfiguration implements Configurable {
    private final static String appIDHint = "请输入appID";
    private final static String securityKeyHint = "请输入securityKey";

    private final JComponent component;

    private final JTextField appID;

    private final JTextField securityKey;

    public TranslatorSettingConfiguration() {
        this.component = new JPanel();
        this.component.setLayout(new GridLayout(15, 1));

        this.appID = new JTextField();
        this.securityKey = new JTextField();

        this.appID.addFocusListener(new TextFieldListener(this.appID, appIDHint));
        if (TranslatorSetting.getInstance().appId != null) {
            this.appID.setText(TranslatorSetting.getInstance().appId);
        } else {
            this.appID.setText(appIDHint);
            this.appID.setForeground(JBColor.GRAY);
        }

        this.securityKey.addFocusListener(new TextFieldListener(this.securityKey, securityKeyHint));
        if (TranslatorSetting.getInstance().securityKey != null) {
            this.securityKey.setText(TranslatorSetting.getInstance().securityKey);
        } else {
            this.securityKey.setText(securityKeyHint);
            this.securityKey.setForeground(JBColor.GRAY);
        }

        this.component.add(this.appID);
        this.component.add(this.securityKey);
    }

    /**
     * 获取配置在Settings/Preferences 中显示的名字
     *
     * @return
     */
    @Override
    public @NlsContexts.ConfigurableName String getDisplayName() {
        return "Translator";
    }

    /**
     * 基于Swing 设计配置界面UI
     *
     * @return
     */
    @Override
    public @Nullable JComponent createComponent() {
        return component;
    }

    /**
     * 提供给IDEA判断配置是否发生变更，若返回true,则配置界面中的apply按钮可点击
     */
    @Override
    public boolean isModified() {
        return true;
    }

    /**
     * 当在配置页面点击 apply 或者 ok 按钮时，该方法会被调用
     *
     * @throws ConfigurationException
     */
    @Override
    public void apply() throws ConfigurationException {
        TranslatorSetting.getInstance().appId = appID.getText();
        TranslatorSetting.getInstance().securityKey = securityKey.getText();
    }

    static class TextFieldListener implements FocusListener {
        private final String defaultHit;

        private final JTextField textField;

        public TextFieldListener(JTextField textField, String defaultHit) {
            this.defaultHit = defaultHit;
            this.textField = textField;
        }

        @Override
        public void focusGained(FocusEvent e) {
            // 获得焦点
            if (textField.getText().equals(defaultHit)) {
                textField.setText("");
                textField.setForeground(JBColor.BLACK);
            }
        }

        @Override
        public void focusLost(FocusEvent e) {
            // 失去焦点
            if (textField.getText().isEmpty()) {
                textField.setText(defaultHit);
                textField.setForeground(JBColor.GRAY);
            }
        }
    }
}
